﻿namespace Neovolve.Extensibility.ReSharper
{
    using System;
    using JetBrains.ReSharper.Psi;
    using JetBrains.ReSharper.Psi.CSharp;
    using JetBrains.ReSharper.Psi.CSharp.Tree;
    using JetBrains.ReSharper.Psi.Tree;

    /// <summary>
    /// The <see cref="TypeUsageNodeDescriptor"/>
    ///   class is used to describe <see cref="ITypeUsageNode"/> instances.
    /// </summary>
    internal class TypeUsageNodeDescriptor : NodeDescriptor
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="TypeUsageNodeDescriptor"/> class.
        /// </summary>
        /// <param name="node">
        /// The node managed by this converter.
        /// </param>
        /// <exception cref="ArgumentNullException">
        /// The <paramref name="node"/> value is <c>null</c>.
        /// </exception>
        public TypeUsageNodeDescriptor(ITreeNode node)
            : base(node)
        {
        }

        /// <summary>
        /// Creates the name of the node for type.
        /// </summary>
        /// <param name="typeName">
        /// Name of the type.
        /// </param>
        /// <returns>
        /// A <see cref="ITreeNode"/> value.
        /// </returns>
        protected override ITreeNode CreateNodeForTypeName(String typeName)
        {
            IPsiModule project = Node.GetPsiModule();

            return CSharpElementFactory.GetInstance(project).CreateTypeUsageNode(typeName);
        }

        /// <summary>
        /// Gets a value indicating whether the node is supported by this descriptor.
        /// </summary>
        /// <value>
        /// <c>true</c> if the node is supported by this descriptor; otherwise, <c>false</c>.
        /// </value>
        public override Boolean IsSupportedNode
        {
            get
            {
                IEnumBaseNode enumBase = Node.Parent as IEnumBaseNode;

                return Node is ITypeUsageNode && enumBase == null;
            }
        }
    }
}